from django.db import models
from django.contrib.auth.models import AbstractUser

# Create your models here.


# 权限类型
class PermissionType(models.Model):
    name = models.CharField("权限类型", max_length=30)

    def __str__(self):
        return self.name

    class Meta:
        db_table = "permission_type"
        verbose_name_plural = '权限类型'


#用户表
class User(AbstractUser):
    """
    用户表
    """
    mobile = models.CharField(max_length=11, default="", verbose_name='用户手机')
    last_login = models.DateTimeField(null=True)
    role = models.ManyToManyField(PermissionType)

    class Meta:
        db_table = 'user'

    def __str__(self):
        return self.username


class Permission(models.Model):
    name = models.CharField("权限名称",max_length=30)
    code_name = models.CharField("权限代码",max_length=300)
    type_name = models.ForeignKey(PermissionType, on_delete=models.CASCADE,verbose_name="权限类型")

    def __str__(self):
        return self.name

    class Meta:
        db_table = "permission"
        verbose_name_plural = "用户权限"


class UserGroup(models.Model):
    name = models.CharField("用户组名",max_length=30)
    user = models.ManyToManyField(User)
    permission = models.ManyToManyField(Permission)

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'user_group'
        verbose_name_plural = "用户组"


class UserAvatar(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="用户")
    avatar = models.CharField("图片路径", max_length=200)

    def __str__(self):
        return self.avatar

    class Meta:
        db_table = 'user_avatar'
        verbose_name_plural = "用户头像"














